//
//  SLSafe.m
//  SLKit
//
//  Created by S_LINE on 2020/9/24.
//  Copyright © 2020 S_LINE. All rights reserved.
//

#import "SLSafe.h"

@implementation SLSafe

/// 参数是字符串时，原样输出，参数是nsnumber类型时，转换为nsstring输出，其他类型输出@""
+ (NSString *)convertNumberToString:(id)obj
{
    if ([SLSafe isEffectiveString:obj])
    {
        return obj;
    }
    else if (obj && [obj isKindOfClass:[NSNumber class]])
    {
        return [NSString stringWithFormat:@"%@", obj];
    }
    else
    {
        return @"";
    }
}

/// 判断传入对象是否是有效字符串
+ (BOOL)isEffectiveString:(id)string
{
    if (!string)
    {
        return NO;
    }
    if (![string isKindOfClass:[NSString class]] && ![string isKindOfClass:NSClassFromString(@"__NSCFConstantString")])
    {
        return NO;
    }
    if (((NSString *)string).length == 0) {
        return NO;
    }
    return YES;
}

/// 判断传入对象是否是有效NSNumber类型数据
+ (BOOL)isEffectiveNumber:(id)number
{
    if (!number)
    {
        return NO;
    }
    if (![number isKindOfClass:[NSNumber class]])
    {
        return NO;
    }
    return YES;
}

/// 判断传入对象是否是有效数组
+ (BOOL)isEffectiveArray:(id)array
{
    if (!array)
    {
        return NO;
    }
    if (![array isKindOfClass:[NSArray class]] || ((NSArray *)array).count == 0)
    {
        return NO;
    }
    return YES;
}

/// 判断传入对象是否是有效字典
+ (BOOL)isEffectiveDictionary:(id)dictionary
{
    if (!dictionary)
    {
        return NO;
    }
    if (![dictionary isKindOfClass:[NSDictionary class]] || ((NSDictionary *)dictionary).allKeys.count == 0)
    {
        return NO;
    }
    return YES;
}

@end
